//
//  MSTypeBView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/22.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSTypeBDetailView.h"
#import "MSCollectionBaseCell.h"

@interface MSTypeBDetailView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong) UIImageView *backImg; // 背景图，当没有列表数据时，直接展示大的背景图

@end

@implementation MSTypeBDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubViews];
        [self addLayoutConstraints];
    }
    return self;
}

- (void)setupSubViews{
    [self addSubview:self.collectionView];
    self.descView = [[MSDescDetailView alloc] init];
    [self addSubview:self.descView];
    [self addSubview:self.backImg];
    [self addLayoutConstraints];
}

- (void)addLayoutConstraints{
    [self.descView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(90);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.descView.mas_top);
    }];
    [self.backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.descView.mas_top);
    }];
}

- (void)setDetailData:(id)detailData{
    self.backImg.hidden = YES;
    self.descView.contentData = detailData;
    if ([detailData isKindOfClass:MSFoodData.class]) {
        MSFoodData *foodData = (MSFoodData *)detailData;
        self.dataArray = foodData.contents;
    }
    // FIXME: 收藏页面的店铺展示用
    if ([detailData isKindOfClass:MSFoodDetailData.class]) {
        MSFoodDetailData *foodDetail = (MSFoodDetailData *)detailData;
        if (foodDetail.covers.count >= 1) {
            self.dataArray = foodDetail.covers;
        } else {
            // 没有图片列表数据，直接展示大图
            self.backImg.hidden = NO;
            [self.backImg setImageWithURL:[NSURL URLWithString:foodDetail.img] placeholder:[UIImage imageWithColor:MSPlaceholderColor]];
        }
    }
    [self.collectionView reloadData];
}

#pragma mark -CollectionViewDelegate and datasource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MSPhotosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MSPhotosCollectionViewCell class]) forIndexPath:indexPath];
    cell.modelData = self.dataArray[indexPath.row];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0,0);
}

#pragma mark -Property
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 11;
        layout.minimumInteritemSpacing = 11;
        layout.itemSize = CGSizeMake(150, 87);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[MSPhotosCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MSPhotosCollectionViewCell class])];
    }
    return _collectionView;
}

- (UIImageView *)backImg {
    if (!_backImg) {
        // FIXME:如果此处的背景图可用的话，需要添加点击放大图片的功能
        _backImg = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:MSPlaceholderColor]];
        _backImg.hidden = YES; // 默认隐藏
    }
    return _backImg;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
